package com.client.qiuqiu;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import android.graphics.Typeface;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;

import com.client.qiuqiu.adapters.MenuFragmentPagerAdapter;
import com.client.qiuqiu.databinding.ActivityMenuBinding;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;

public class Menu extends AppCompatActivity {

    private ActivityMenuBinding binding;
    private BottomNavigationView bottomNavigationView;
    private ViewPager2 viewPager2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMenuBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        //viewpager
        initPager();
        viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback(){
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                onPagerSelected(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }

        });

        //底部导航
        bottomNavigationView = binding.bottomNavigationView;
        bottomNavigationView.setOnNavigationItemSelectedListener(bottomItemListener);
        loadUserDetails();
        /*
        //导航页数字，红点
        BadgeDrawable badge = bottomNavigationView.getOrCreateBadge(R.id.nav_item_friend);
        badge.setNumber(10);
        //配合滑动
        bottomNavigationView.removeBadge(R.id.nav_item_friend);
        */
    }

    private void onPagerSelected(int position) {
        switch (position){
            case 0:
                bottomNavigationView.setSelectedItemId(R.id.nav_item_friend);
                break;
            case 1:
                bottomNavigationView.setSelectedItemId(R.id.nav_item_add);
                break;
        }
    }

    private void initPager() {
        viewPager2 = binding.menuViewpager;
        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(new FriendListFragment());
        fragments.add(new AddFriendFragment());
        MenuFragmentPagerAdapter pagerAdapter = new MenuFragmentPagerAdapter(getSupportFragmentManager(), getLifecycle(), fragments);
        viewPager2.setAdapter(pagerAdapter);
    }

    private void loadUserDetails(){
        User user = ((User)getApplicationContext());
        binding.textName.setText(user.getName());
        binding.textName.setTypeface(Typeface.createFromAsset(getAssets(), "font/Times_New_Roman.ttf"));
    }

    private void showToast(String message){
        Toast.makeText(Menu.this, message, Toast.LENGTH_SHORT).show();
    }

    private BottomNavigationView.OnNavigationItemSelectedListener bottomItemListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()){
                case R.id.nav_item_friend:
                    viewPager2.setCurrentItem(0);
                    break;
                case R.id.nav_item_add:
                    viewPager2.setCurrentItem(1);
                    break;
            }
            return true;
        }
    };

}